package com.dopplr.api
{
    /** represents a Traveller's trip on Dopplr
     *  @see Model#getTripFromXML
     */
    public class Trip
    {
        public var id:int;      // 64

        public var start:Date;  // 2007-01-20
        public var finish:Date; // 2007-01-21

        public var city:City;
        public var departure:City; // not sure how this is calculated yet
        
        public var notes:Array = []; // array of Notes, may be empty
        
        /** @return true if this trip will coincide with the other 
         *  ie they are to the same city and overlap */
        public function coincides(other:Trip):Boolean
        {
            return this.city == other.city && this.overlaps(other);
        }
        
        /** @return true if the start and finish dates of this trip and the other overlap */
        public function overlaps(other:Trip):Boolean
        {
            // deep breath...
            // either other.start is between this.start and this.finish
            //     or other.finish is between this.start and this.finish
            //     or other.start is before this.start and other.finish is after this.finish
            // anything else, or am I being silly?
            return (other.start.time  >= this.start.time && other.start.time  <= this.finish.time)
                || (other.finish.time >= this.start.time && other.finish.time <= this.finish.time)
                || (other.start.time  <= this.start.time && other.finish.time >= this.finish.time);
        }
        
        public function get days():int
        {
            var millisInDay:Number = 24 * 60 * 60 * 1000;
            return Math.floor((finish.time - start.time + millisInDay) / millisInDay);
        }
        
        public function toString():String
        {
            return days.toString() + " day trip to " + city.name;
        }
        
    }
}